--[[
登陆场景
]]
local RoleModel = require("app.model.RoleModel")
local BaseScene = require("engin.mvcs.view.BaseScene")
local LoginScene = class("LoginScene", BaseScene)



function LoginScene:ctor(param)
	if device.platform ~= "windows" then self:init(); return end
	

	--[[
		CCNetworkStatusNotReachable: 无法访问互联网
	   	kCCNetworkStatusReachableViaWiFi: 通过 WIFI
	   	kCCNetworkStatusReachableViaWWAN: 通过 3G 网络
	]]
	--状态
	local networkStatus = RoleModel.new();
	if (networkStatus == kCCNetworkStatusReachableViaWiFi or  networkStatus == kCCNetworkStatusReachableViaWWAN)
	then
		if (RoleModel.UseName == nil or RoleModel.Password == nil ) then
			self:init();
		else
			self:Login(RoleModel.UseName,RoleModel.Password)
		end
	else --没联网情况下
		local function onButtonClicked(event)
			echoj("进入世界场景",event.buttonIndex );
			if event.buttonIndex == 1 then--玩家选择了 YES 按钮
				self:performWithDelay(function()
					app:enterScene(SceneConstants.WorldLevelScene())
					--self:init();
				end, 0.4)
		    else
		    	self:init();
		    end
		end
		device.showAlert("提示", "当前没有网络，是否单机游戏 ?", {"YES", "NO"}, onButtonClicked)
	end
	echoj("当前联网状态",networkStatus);

end
	



--登陆service
function LoginScene:init(param)
	cc.FileUtils:getInstance():addSearchPath("res/login/")
  	local node = cc.CSLoader:createNode("login/login.csb",function(object)
        if nil ~= object then
        end
    end);
    node:addTo(self);
    self.node_ = node;
	node:setContentSize(cc.size(display.width,display.height));
	ccui.Helper:doLayout(node)
		
		
	--这里就报错了    获取子元件  
	local Button_Login = node:getChildByTag(93);
	local Button_Logout =node:getChildByName("Button_Logout");
	echoj(Button_Logout,Button_Login);--这里打印为nil
	Button_Logout:addTouchEventListener(handler(self,self.loginOutHandle))
	
	
	--用户名
	--[[
	local Button_Login = node:getChildByName("Button_Login");
	local Button_Login = cc.uiloader:seekNodeByTag(self, 93);
	local Button_Login = node:getTag(93);
	]]
	local Button_Login = node:getChildByName("Button_Login");
	Button_Login:addTouchEventListener(handler(self,self.loginHandle))
	
	
	
	
	local Text_Version =node:getChildByName("Text_Version");
	Text_Version:addTouchEventListener(function(sender,eventType)  
		if eventType == ccui.TouchEventType.ended then
			local v= "v1.0"
			if DEBUG_MAP ~= 0  then
				DEBUG_MAP = 0;
				Text_Version:setString(v);
			else
				DEBUG_MAP = 2;
				Text_Version:setString(v.."_edit");
			end
		end
	end)
	
	
			
   	local function selectedStateEvent(sender,eventType)
        --[[
        if eventType == ccui.CheckBoxEventType.selected then
        elseif eventType == ccui.CheckBoxEventType.unselected then
        end
        ]]
    end
	local CheckBox_Sava_Password = node:getChildByName("CheckBox_Sava_Password");
	CheckBox_Sava_Password:addEventListener(selectedStateEvent)
	
	
	--[[
	local atlasParam = {
		labelWithString = "123",
	}
	local txt = AtlasText.create(atlasParam);
	self:addChild(txt);
	txt:setPosition(display.cx,display.cy)
	]]
end




function LoginScene:Login(UseName,Password)
	local loginCallBack = function (result)
		local status = result['status'] --后台返回的数据成功与否的编码
		if status == 1 then 
			app:enterScene(SceneConstants.WorldLevelScene())
		end
	end
	loginCallBack({status =1});--测试
	RoleModel({UseName = UseName,Password = Password},loginCallBack);
end









--[[
 登陆
]]
function LoginScene:loginHandle(sender,eventType)
	if eventType == ccui.TouchEventType.began then
           
    elseif eventType == ccui.TouchEventType.moved then
       
    elseif eventType == ccui.TouchEventType.ended then
    	local TextField_UseName = self.node_ :getChildByName("TextField_UseName");
		local TextField_Password = self.node_ :getChildByName("TextField_Password");
        local UseName = TextField_UseName:getString();
        local Password = TextField_Password:getString();
	
		self:Login(UseName,Password);
    elseif eventType == ccui.TouchEventType.canceled then
      
    end
end



--登出
function LoginScene:loginOutHandle(sender,eventType)
	if eventType == ccui.TouchEventType.ended then
		app.exit();
	end
end




return LoginScene
